package com.java2020.online_mall.controller;

import com.google.gson.Gson;
import com.java2020.online_mall.model.Admin;
import com.java2020.online_mall.model.AdminPwd;
import com.java2020.online_mall.model.Result;
import com.java2020.online_mall.service.AdminService;
import com.java2020.online_mall.service.AdminServiceImpl;
import com.java2020.online_mall.utils.HttpUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @PackgeName: ${PACKAGE_NAME}
 * @Author: MNHK
 * Date: 2020/4/28 22:34
 * @Version:1.8
 */
@WebServlet("/api/admin/admin/*")
public class AdminServlet extends javax.servlet.http.HttpServlet {

    private AdminService adminService = new AdminServiceImpl();
    Gson gson = new Gson();
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        String requestURI = request.getRequestURI();
        String correct = requestURI.replace("/api/admin/admin/", "");
        if ("login".equals(correct)){
            login(request,response);
        }else if("getSearchAdmins".equals(correct)){
            getSearchAdmins(request, response);
        }else if("addAdmins".equals(correct)){
            addAdmins(request, response);
        }else if("updateAdmins".equals(correct)){
            updateAdmins(request, response);
        }else if("changePwd".equals(correct)) {
            changePwd(request, response);
        }else if ("logout".equals(correct)){
            logout(request, response);
        }
    }

    /**
     * 退出登录
     * @param request
     * @param response
     */
    private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().invalidate();
        Result result = new Result();
        result.setCode(0);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 修改密码
     * @param request
     * @param response
     */
    private void changePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AdminPwd adminPwd = gson.fromJson(requestBody, AdminPwd.class);
        int code = adminService.changePwd(adminPwd);
        Result result = new Result();

        if(code == 200){
            result.setCode(0);
        }else if(code == 404){
            result.setCode(10000);
            result.setMessage("旧密码错误");
        }else if(code == 501){
            result.setCode(10000);
            result.setMessage("请保持确认新密码一致");
        }else {
            result.setCode(10000);
            result.setMessage("服务器异常");
        }
        response.getWriter().println(gson.toJson(result));
    }

    private void updateAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody, Admin.class);
        int code = adminService.updateAdmins(admin);
        Result result = new Result();

        if(code == 200){
            result.setCode(0);
        }else if(code == 404) {
            result.setCode(10000);
            result.setMessage("账号未注册");
        }else if(code == 501){
            result.setMessage("该账号不允许重复使用");
            result.setCode(10000);
        }else if(code == 502){
            result.setMessage("该用户名不允许重复使用");
            result.setCode(10000);
        }else {
            result.setCode(10000);
            result.setMessage("服务器异常");
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 添加管理员
     * @param request
     * @param response
     */
    private void addAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody, Admin.class);
        int code = adminService.addAdmins(admin);

        Result result = new Result();
        if(code == 200){
            //说明添加成功
            result.setCode(0);
        }else if(code == 501){
            result.setMessage("该账号异常");
            result.setCode(10000);
        }else {
            result.setCode(10000);
            result.setMessage("服务器异常");
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 多条件查询管理员账号的逻辑
     * @param request
     * @param response
     */
    private void getSearchAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Admin params = gson.fromJson(requestBody, Admin.class);
        List<Admin> searchAdmins = adminService.getSearchAdmins(params);
        Result result = new Result();
        result.setCode(0);
        result.setData(searchAdmins);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 登录的具体业务逻辑
     * @param request
     * @param response
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody, Admin.class);
        int code = adminService.login(admin);
        Result result = new Result();
        if (code == 200){
            result.setCode(0);
            HashMap<Object, Object> map = new HashMap<>();
            map.put("token",admin.getEmail());
            map.put("name",admin.getEmail());
            result.setData(map);
        }else if (code == 404) {
            result.setCode(10000);
            result.setMessage("资源找不到");
        }else {
            result.setCode(10000);
            result.setMessage("服务器异常");
        }
        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        String requestURI = request.getRequestURI();
        String correct = requestURI.replace("/api/admin/admin/", "");
        if("allAdmins".equals(correct)){
            allAdmins(request, response);
        }else if("deleteAdmins".equals(correct)){
            deleteAdmins(request, response);
        }else if("getAdminsInfo".equals(correct)){
            getAdminsInfo(request, response);
        }
    }

    /**
     * 获取管理员信息
     * @param request
     * @param response
     */
    private void getAdminsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Admin admin = adminService.getAdminsInfo(id);
        Result result = new Result();
        result.setCode(0);
        result.setData(admin);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 删除管理员
     * @param request
     * @param response
     */
    private void deleteAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int code = adminService.deleteAdmins(id);
        Result result = new Result();
        if (code == 200) {
            result.setCode(0);
        } else if (code == 404) {
            result.setCode(10000);
            result.setMessage("找不到请求资源");
        }else if (code == 501) {
            result.setCode(10000);
            result.setMessage("数据库异常！");
        }else {
            result.setCode(10000);
            result.setMessage("服务器异常！");
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 查看所有管理员信息
     * @param request
     * @param response
     */
    private void allAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Admin> admins = adminService.allAdmins();
        Result result = new Result();
        result.setCode(0);
        result.setData(admins);
        response.getWriter().println(gson.toJson(result));
    }
}
